package io.renren.modules.commodity.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.behavior.entity.UserBehaviorLogEntity;
import io.renren.modules.commodity.entity.CommodityEntity;
import io.renren.modules.commodity.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品表
 *
 * @author xianwenchao
 * @email 1140617792@qq.com
 * @date 2021-02-12 22:12:07
 */
@RestController
@Api(value = "商品表")
@RequestMapping("commodity/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    /**
     * 商品表列表
     */
    @GetMapping("/list")
    @RequiresPermissions("commodity:commodity:list")
    @ApiOperation(value = "查询商品表列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = commodityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据id查询商品表信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("commodity:commodity:info")
    @ApiOperation(value = "根据id查询商品表信息")
    public R info(@PathVariable("id") Long id) {
        CommodityEntity commodity = commodityService.queryById(id);

        return R.ok().put("commodity", commodity);
    }

    /**
     * 新增商品表记录
     */
    @PostMapping("/save")
    @RequiresPermissions("commodity:commodity:save")
    @ApiOperation(value = "新增商品表记录")
    public R save(@RequestBody CommodityEntity commodity) {
        commodityService.save(commodity);

        return R.ok();
    }

    /**
     * 根据id修改商品表记录
     */
    @PostMapping("/update")
    @RequiresPermissions("commodity:commodity:update")
    @ApiOperation(value = "根据id修改商品表信息")
    public R update(@RequestBody CommodityEntity commodity) {
        commodityService.updateById(commodity);

        return R.ok();
    }

    /**
     * 用户点赞或取消点赞商品
     *
     * @param id 商品id
     */
    @PostMapping("/like")
    @ApiOperation(value = "用户点赞商品")
    public R likeItem(@RequestParam("id") Long id) {

        commodityService.userLikeOrDislike(id);

        return R.ok();
    }

    /**
     * 用户收藏或取消收藏商品
     *
     * @param id 商品id
     */
    @PostMapping("/collection/{id}")
    @ApiOperation(value = "用户收藏商品")
    public R collectionItem(@PathVariable("id") Long id) {

        commodityService.userCollectionOrNot(id);

        return R.ok();
    }

    /**
     * 用户评论商品
     *
     * @param userBehaviorLogEntity 用户行为
     */
    @PostMapping("/comment")
    @ApiOperation(value = "用户评论商品")
    public R commentItem(@RequestBody UserBehaviorLogEntity userBehaviorLogEntity) {

        commodityService.userCommentItem(userBehaviorLogEntity);

        return R.ok();
    }

    /**
     * 用过商品id查找所有评论
     *
     * @param itemId 商品id
     */
    @GetMapping("/comment/{itemId}")
    @ApiOperation(value = "商品所有评论")
    public R itemCommentList(@PathVariable("itemId") Long itemId) {

        List<UserBehaviorLogEntity> list = commodityService.allItemComment(itemId);

        return R.ok().put("list", list);
    }

    /**
     * 用过商品id查找所有评论
     */
    @GetMapping("/guess/like")
    @ApiOperation(value = "商品所有评论")
    public R guessUserLike() {

        List<CommodityEntity> commodityEntities = commodityService.guessUserLike();
        if (commodityEntities == null || commodityEntities.size() == 0) {
            return R.ok("暂无推荐商品，请先到处逛逛吧~");
        }

        return R.ok();
    }

    /**
     * 根据id删除商品表记录
     */
    @PostMapping("/delete")
    @RequiresPermissions("commodity:commodity:delete")
    @ApiOperation(value = "根据id删除商品表记录(非逻辑删除，建议不使用)")
    public R delete(@RequestBody Long[] ids) {
        commodityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
